package zhongbo.javaweb.instagram.dao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import zhongbo.javaweb.instagram.model.BaseModel;
import zhongbo.javaweb.instagram.service.FileService;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 说明：
 *
 * @author zhongboWork
 * @version 1.0
 * @since 2021-12-21 10:59
 */
@Slf4j
public abstract class BaseDao<MODEL extends BaseModel> implements IBaseDao<MODEL> {

    Class<MODEL> tClass;

    @Autowired
    private FileService<MODEL> fileService;

    public BaseDao(Class<MODEL> tClass) {
        this.tClass = tClass;
    }

    @Override
    public MODEL getById(String byId) {
        try {
            List<MODEL> modelJson = fileService.readJsonList(tClass);
            for (MODEL model : modelJson) {
                String id = model.getId();
                if (byId.equals(id)) {
                    return model;
                }
            }
        } catch (Exception e) {
            String msg = String.format("读取 %s 数据文件失败", tClass.getSimpleName());
            throw new RuntimeException(msg, e);
        }
        return null;
    }

    @Override
    public MODEL removeById(String id) {
        List<MODEL> models = getList();
        if (!models.isEmpty()) {
            MODEL remove = null;
            for (MODEL model : models) {
                if (id.equals(model.getId())) {
                    remove = model;
                    break;
                }
            }
            models.remove(remove);
            try {
                fileService.writeJsonList(models, tClass);
                return remove;
            } catch (IOException e) {
                String msg = String.format("写入 %s 数据文件失败 file=%s", tClass.getSimpleName(), fileService.getUserDirFile(tClass));
                throw new RuntimeException(msg, e);
            }
        }
        return null;
    }

    @Override
    public MODEL update(MODEL model) {
        if (model != null) {
            String id = model.getId();
            if (StringUtils.isEmpty(id)) {
                id = UUID.randomUUID().toString();
                model.setId(id);
            }
            List<MODEL> models = getList();
            if (!models.isEmpty()) {
                int i = 0;
                boolean has = false;
                for (; i < models.size(); i++) {
                    MODEL model1 = models.get(i);
                    if (id.equals(model1.getId())) {
                        has = true;
                        break;
                    }
                }
                if (has) {
                    model.setUpdatedTime(System.currentTimeMillis());
                    models.remove(i);
                }
            }
            models.add(model);
            try {
                fileService.writeJsonList(models, tClass);
                return model;
            } catch (IOException e) {
                String msg = String.format("写入 %s 数据文件失败 file=%s", tClass.getSimpleName(), fileService.getUserDirFile(tClass));
                throw new RuntimeException(msg, e);
            }
        }
        return null;
    }

    @Override
    public List<MODEL> getList() {
        try {
            List<MODEL> models = fileService.readJsonList(tClass);
            // 降序排列
            models.sort((a, b) -> (int) (b.getUpdatedTime() - a.getUpdatedTime()));
            return models;
        } catch (IOException e) {
            String msg = String.format("读取 %s 数据文件失败 file=%s", tClass.getSimpleName(), fileService.getUserDirFile(tClass));
            throw new RuntimeException(msg, e);
        }
    }
}
